package org.example.myerp.service.serviceimpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.example.myerp.dto.RoleDto;
import org.example.myerp.mapper.RoleMapper;
import org.example.myerp.pojo.PageMyself;
import org.example.myerp.pojo.Role;
import org.example.myerp.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RoleServiceImpl implements RoleService {

	@Autowired
    RoleMapper roleMapper;

	@Override
	public PageMyself<Role> findByPage(Integer pageNum, Integer pageSize) {
		Map params = new HashMap<String, Object>();
		params.put("pageNum", pageNum);
		params.put("pageSize", pageSize);
		PageMyself<Role> page = new PageMyself<Role>();
		page.setPageNum(pageNum);
		page.setPageSize(pageSize);
		List<Role> lis = roleMapper.findByPager(params);
		page.setRows(lis);
		page.setTotal(roleMapper.pageTotal());
		return page;
	}

	@Override
	public void addRole(Role role) {
		roleMapper.addRole(role);

	}

	@Override
	public void updateRole(Role role) {
		roleMapper.updateRole(role);
	}

	@Override
	public void batchDelRole(int[] rids) {
		roleMapper.batchDelRole(rids);

	}

	@Override
	public void setRoleMenu(Integer rid, Integer[] mids) {
		if (mids.length > 0) {
			roleMapper.delRoleMenu(rid);
			for (int i = 0; i < mids.length; i++) {
				roleMapper.addRoleMenu(rid, mids[i]);
			}
		} else {
			roleMapper.delRoleMenu(rid);

		}
	}

	@Override
	public RoleDto roleDto(Integer uid) {
		RoleDto rd = new RoleDto();
		rd.setRoles(roleMapper.allRole());
		rd.setUserRids(roleMapper.userRids(uid));
		return rd;
	}

}
